﻿//*地址关联层脚本*/ by windbell2
/****************************************/
//onclick="showAddressTip(this,'city')"
//onclick = "showAddressTip(this,'area','city')"
//onclick="showAddressTip(this,'street','area')"
/****************************************/


//xml常量
var $xmldoc
//load xml
function loadAddress() {
    $(document.body).append('<div id="addressTipLayer" class="addressTipLayer" targetID="" onclick="putAddressValue(event)" ></div>')
    $.ajax({
        url: "/content/address.xml",
        processData: false,
        dataType: "xml",
        success: function (ss) {
            $xmldoc = $(ss);

        }
    });
}
//show address tip
function getAddressArray(type, n) {
    var arr = new Array();
    var _arr = new Array();



    switch (type) {
        case "city":
            _arr = $xmldoc.children().children();

            break;
        case "area":
            _arr = $xmldoc.find("city[n=" + n + "]").children();

            break;
        case "street":
            _arr = $xmldoc.find("area[n=" + n + "]").children();
        default:
            break;
    }
    if (_arr != null) {
        _arr.each(function () { arr.push($(this).attr("n")); });
    }
    return arr;
}
//提取数据，显示层
function showAddressTip(target, type, pTarget) {

    if ($xmldoc == null) { return; }
    if (type == undefined || type == "") { return; }

    var n = $("#" + pTarget).val();

    if (type != "city" && (n == undefined || n.trim() == "")) { return; }

    var addressArr = getAddressArray(type, n);
    var $it = $("#addressTipLayer");
    $it.attr("targetID", type)
    $it.html("");
    var off = $(target).offset();
    for (var i in addressArr) {
        $it.append("<a href='###'>" + addressArr[i] + "</a>");

    }
    $it.css({ "top": off.top + $(target).height()+6 });
    $it.css({ "left": off.left, width: $(target).width()-6 });
    $it.show();

}
//写出选中值
function putAddressValue(evt) {
    var e = $.event.fix(evt);

    if (e.srcElement.tagName == "A") {

        $("#" + $("#addressTipLayer").attr("targetID")).val(e.srcElement.innerHTML);
        $("#addressTipLayer").hide();
    }
}
//!!!!!!!!!!即可出始化
$(loadAddress);
/*地址关联层脚本*/